import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/works_2023/report_loss_bean.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/pay_free.dart';
import 'package:app/src/http/bean/wroks/pay_notice_bean.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/receipt_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../../http/bean/works_2023/material_review_info.dart';
import '../../../../../http/bean/wroks/payfree_return_bean.dart';
import '../../../../app_ui.dart';

///五金领用审核；列表信息
class UiHardwareApprovalAdapter {
  static Widget build(BuildContext context, List<MaterialReviewInfoBean>? datas,
      {String? title}) {
    if (StringUtils.isEmpty(datas)) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).Listinformation; //列表信息
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(
      context,
      datas,
    ));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(
    BuildContext context,
    List<MaterialReviewInfoBean>? datas,
  ) {
    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        List<TableRow> children = [];
        //==========================================================================标题
        children.add(TableRow(
            //第一行，添加背景色
            decoration:
                BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
            children: _titles(context)));
        if (datas != null && datas.length > 0) {
          var postion = 1;
          datas.forEach((element) {
            //======================================================================内容
            var cells = _contents(context, postion, element, setState);
            postion++;
            children.add(TableRow(children: cells));
          });
        }
        var table = Table(
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          columnWidths: {
            0: FixedColumnWidth(50),
            // 1: FixedColumnWidth(150),
            // 2: FixedColumnWidth(200),
            // 3: FixedColumnWidth(300),
            // 4: FixedColumnWidth(200),
          },
          textDirection: TextDirection.ltr,
          border: AppStyle.getTableBorder(),
          children: children,
        );
        var width = 2000.0;
        if (width < px.screenWidth()) {
          width = px.screenWidth();
        }
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          physics: BouncingScrollPhysics(),
          child: Container(
            width: width,
            child: table,
          ),
        );
      },
    );
  }

  ///标题
  static List<Widget> _titles(BuildContext context) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    children.add(_addItem(AppStrings.of(context).MaterName)); //五金名称
    children.add(_addItem(AppStrings.of(context).ManufacturerName)); //品牌
    children.add(_addItem(AppStrings.of(context).Width_Height)); //规格型号
    children.add(_addItem(AppStrings.of(context).EquipmentName)); //关联设备
    children.add(_addItem(AppStrings.of(context).BasicNumUnit)); //单位数量
    children.add(_addItem(AppStrings.of(context).YearPickSetNum)); //年限用数
    children.add(_addItem(AppStrings.of(context).MonthPickSetNum)); //月限用数
    children.add(_addItem(AppStrings.of(context).CountPickSetNum)); //次限用数
    children.add(_addItem(AppStrings.of(context).HasFeedBasicNum)); //已领用数量
    children.add(_addItem(AppStrings.of(context).PlanBasicNum)); //申请数量
    children.add(_addItem(AppStrings.of(context).Trade_in)); //是否以旧换新
    children.add(_addItem(AppStrings.of(context).Remarks)); //备注
    return children;
  }

  ///内容
  static List<Widget> _contents(BuildContext context, int postion,
      MaterialReviewInfoBean element, StateSetter setState) {
    List<Widget> children = [];
    children.add(_addItem((postion).toString())); //序号
    children.add(_addItem(element.MaterName)); //五金名称
    children.add(_addItem(element.ManufacturerName)); //品牌
    children.add(_addItem(element.Size)); //规格型号
    children.add(_addItem(element.EquipmentName)); //关联设备
    children.add(_addItem(
        "${element.BasicNum}${element.BasicUnit}/${element.PackageUnit}")); //单位数量
    children
        .add(_addItem("${element.YearPickSetNum}${element.BasicUnit}")); //年限用数
    children
        .add(_addItem("${element.MonthPickSetNum}${element.BasicUnit}")); //月限用数
    children
        .add(_addItem("${element.CountPickSetNum}${element.BasicUnit}")); //次限用数
    children.add(
        _addItem("${element.HasFeedBasicNum}${element.BasicUnit}")); //已领用数量
    var PlanBasicNum = UiBase.itemWithSigleEdit(context, element?.PlanBasicNum,
        isAllowEmpty: true,
        isDecimal: true,
        isNegative: false,
        unit: element?.BasicUnit ?? "",
        isLine: true, onChanged: (value) {
      element?.PlanBasicNum = value;
    });
    children.add(PlanBasicNum); //申请数量

    if (StringUtils.isEmpty(element.isOldNew)) {
      children.add(AppUi.getEmptyView());
    } else {
      children.add(StatefulBuilder(builder: (ctx, state) {
        var b = BoolUtils.isBoolWithStr(element.isOldNew);
        return Checkbox(
            value: b,
            onChanged: (value) {
              state(() {
                // print("========$value");
                element.isOldNew = value.toString() ?? "false";
              });
            });
      })); //是否以旧换新
    }

    children.add(_addItem("${element.Remarks}")); //备注

    return children;
  }

  static Widget _addItem(String? text,
      {bool isBlue = false, GestureTapCallback? onTap, String? hint}) {
    Widget child = Center(child: _text(text, isBlue: isBlue, hint: hint));
    if (onTap == null) {
      return child;
    }
    return InkWell(
      child: child,
      onTap: onTap,
    );
  }

  static Widget _text(String? text, {bool isBlue = false, String? hint}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    var textStr = text?.trim() ?? "";
    if (StringUtils.isEmpty(textStr) && hint != null) {
      textStr = hint.trim();
    }
    return Text(
      textStr,
      style: style,
    );
  }
}
